package com.ms.education.course.handler;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project:education-StpInterfaceImpl
 * <p>Powered by ms on 2023-03-12 14:35:48
 * 获取当前找好权限集合
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@Component
public class StpInterfaceImpl implements StpInterface {



    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissionList = new ArrayList<>();
        List<String> roles = getRoleList(loginId, loginType);
        for (String role : roles) {
            SaSession roleSession = SaSessionCustomUtil.getSessionById("role-" + role);
            List<String> permissions = (List<String>) roleSession.get("Permission_List");
            permissionList.addAll(permissions);
        }
        return permissionList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        SaSession session = StpUtil.getSessionByLoginId(loginId);
        return (List<String>) session.get("Role_List");
    }
}
